package com.childenglish.controller;

import com.childenglish.utils.DataEncryptionUtils;
import com.childenglish.utils.R;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 数据加密演示控制器
 * 用于演示和测试数据加密功能
 */
@RestController
@RequestMapping("/api/encryption")
public class EncryptionController {

    /**
     * 加密数据演示
     */
    @PostMapping("/encrypt")
    public R encryptData(@RequestBody Map<String, String> request) {
        try {
            String data = request.get("data");
            if (data == null || data.isEmpty()) {
                return R.error("数据不能为空");
            }

            String encrypted = DataEncryptionUtils.encryptSensitiveField(data);
            
            Map<String, Object> result = new HashMap<>();
            result.put("original", data);
            result.put("encrypted", encrypted);
            result.put("algorithm", "AES-256");
            result.put("note", "加密后的数据可以安全存储或传输");

            return R.ok("数据加密成功").data(result);
        } catch (Exception e) {
            return R.error("数据加密失败: " + e.getMessage());
        }
    }

    /**
     * 解密数据演示
     */
    @PostMapping("/decrypt")
    public R decryptData(@RequestBody Map<String, String> request) {
        try {
            String encryptedData = request.get("encryptedData");
            if (encryptedData == null || encryptedData.isEmpty()) {
                return R.error("加密数据不能为空");
            }

            String decrypted = DataEncryptionUtils.decryptSensitiveField(encryptedData);
            
            Map<String, Object> result = new HashMap<>();
            result.put("encrypted", encryptedData);
            result.put("decrypted", decrypted);
            result.put("algorithm", "AES-256");
            result.put("note", "解密后的数据是原始明文");

            return R.ok("数据解密成功").data(result);
        } catch (Exception e) {
            return R.error("数据解密失败: " + e.getMessage());
        }
    }

    /**
     * 加密/解密演示（同时进行）
     */
    @PostMapping("/demo")
    public R encryptionDemo(@RequestBody Map<String, String> request) {
        try {
            String data = request.get("data");
            if (data == null || data.isEmpty()) {
                return R.error("数据不能为空");
            }

            // 加密
            String encrypted = DataEncryptionUtils.encryptSensitiveField(data);
            
            // 解密
            String decrypted = DataEncryptionUtils.decryptSensitiveField(encrypted);
            
            Map<String, Object> result = new HashMap<>();
            result.put("original", data);
            result.put("encrypted", encrypted);
            result.put("decrypted", decrypted);
            result.put("encryptionAlgorithm", "AES-256");
            result.put("encryptionMode", "ECB");
            result.put("padding", "PKCS5Padding");
            result.put("keyLength", "256 bits");
            result.put("isMatch", data.equals(decrypted));
            result.put("note", "加密和解密使用相同的密钥，确保数据可以正确还原");

            return R.ok("加密解密演示完成").data(result);
        } catch (Exception e) {
            return R.error("操作失败: " + e.getMessage());
        }
    }

    /**
     * 获取加密信息（不包含敏感数据）
     */
    @GetMapping("/info")
    public R getEncryptionInfo() {
        Map<String, Object> info = new HashMap<>();
        info.put("algorithm", "AES");
        info.put("keyLength", "256 bits");
        info.put("mode", "ECB");
        info.put("padding", "PKCS5Padding");
        info.put("keySource", "环境变量 DATA_ENCRYPTION_KEY 或默认密钥");
        info.put("jasyptEnabled", true);
        info.put("jasyptAlgorithm", "PBEWithMD5AndDES");
        info.put("usage", "用于加密敏感数据，如手机号、邮箱、密码等");
        info.put("note", "生产环境请使用环境变量配置加密密钥");

        return R.ok("加密配置信息").data(info);
    }
}

